package com.uwo.house.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.uwo.house.beans.User;
import com.uwo.house.beans.request.*;
import com.uwo.house.beans.response.MeasureOrderResp;
import com.uwo.house.beans.response.SupervisionOrderResp;
import com.uwo.house.config.JWTAuthenticationToken;
import com.uwo.house.kits.CacheKits;
import com.uwo.house.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.uwo.house.util.StringUtil;
import com.uwo.house.util.UtilReturn;

/**
 * 监理中心controller
 * @author Young
 *
 */
@Controller
@RequestMapping("/supervision")
public class SupervisionController extends BaseController{
	@Autowired
	private ISupervisionService supervisionService;

	@Autowired
	private IProjectManagerService projectManagerService;

	@Autowired
	private IDesignCenterService designCenterService;

	@Autowired
	private IUserService userService;

	@Autowired
	private IOrderMeasureService orderMeasureService;

	@Autowired
	private IDecorationSchemeService decorationSchemeService;

	@Autowired
	private IMaterialListService materialListService;

	/**
	 * 设计中心绑定监理
	 * @param supervision
	 * @return
	 */
	@RequestMapping(value = "/bind", method = RequestMethod.POST)
	@ResponseBody
	public Map createSupservisionOrder(@RequestBody BindSupervisionReq supervision) {
		if(StringUtil.isEmpty(supervision.getOrderId())) {
			return UtilReturn.resErrorMap("订单编号不能为空！", UtilReturn.ERROR_CODE);
		}
		supervisionService.bindSupervisionOrder(supervision);
		return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_MESSAGE, UtilReturn.SUCCESS_CODE, null);
	}
	
	/*
	 *老代码
	 * 监理订单查询
	 * @param orderId
	 * @param customerCount
	 * @param status
	 * @param page
	 * @param pageSize
	 * @return
	/*@RequestMapping(value = "/searchOrder", method = RequestMethod.GET)
	@ResponseBody
	public Map searchSupervisionOrder(@RequestParam(value = "orderId", required = false) String orderId,
			@RequestParam(value = "customerCount", required = false) String customerCount,
			@RequestParam(value = "status") int status,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		SupervisionSearchReq searchFilter= new SupervisionSearchReq();
		searchFilter.setOrderId(orderId);
		searchFilter.setCustomerCount(customerCount);
		searchFilter.setStatus(status);
		searchFilter.setPage(page);
		searchFilter.setPageSize(pageSize);
		
		Map<String, Object>  result = supervisionService.getSupervisionOrderSelective(searchFilter);
		return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_MESSAGE, UtilReturn.SUCCESS_CODE, result);
	}*/

	/**
	 * 监理订单查询
	 * @param orderId
	 * @param customerCount
	 * @param status
	 * @param token
     * @return
     */
	@RequestMapping(value = "/searchOrder", method = RequestMethod.GET)
	@ResponseBody
	public Map searchSupervisionOrder(@RequestParam(value = "orderId", required = false) String orderId,
									  @RequestParam(value = "customerCount", required = false) String customerCount,
									  @RequestParam(value = "status", required = false) Integer status,
									  @RequestHeader(value = "Authorization") String token) {
		SupervisionSearchReq searchFilter= new SupervisionSearchReq();
		String[] strs = token.split(" ");
		token = (strs.length == 2) ? strs[1] : null;
		JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
		User user = jwtToken.getUser();
		String userId = user.getId();
		searchFilter.setUserId(userId);
		searchFilter.setOrderId(orderId);
		searchFilter.setCustomerCount(customerCount);
		searchFilter.setStatus(status);
		List<SupervisionOrderResp>  result = supervisionService.getSupervisionOrderSelective(searchFilter);
		for (SupervisionOrderResp sor : result) {
			String designerId = sor.getDesigner();
			User user1 = userService.selectUserById(designerId);
			if (null != user1) {
				sor.setDesignerName(user1.getUsername());
			}
		}
		return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_MESSAGE, UtilReturn.SUCCESS_CODE, result);
	}

	/**
	 * 技术资料详情
	 * @param userId
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value = "/technologyInfo", method = RequestMethod.POST)
	@ResponseBody
	public Map technologyInfo(@RequestParam("orderId") String orderId,
							  @RequestParam(value = "userId") String userId) {
		MeasureCenterReq measureCenterReq = designCenterService.getOrderSpecialInfo(orderId);
		DesignSchemeReq designSchemeReq = projectManagerService.findDesignScheme(orderId,userId);
		List<MaterialListReq> materialList = materialListService.getMaterialDetailByOrderId(orderId);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("measureCenterReq",measureCenterReq);
		map.put("designSchemeReq",designSchemeReq);
		map.put("materialList",materialList);
		return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, map);
	}
	/**
	 * 监理人员上传报告(暂时保存)
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/uploadReport" , method = RequestMethod.POST)
	public Map uploadReport(@RequestParam(value = "orderId") String orderId,
							@RequestParam(value = "reportName") String reportName,
							@RequestParam(value = "reportPicture") String reportPicture,
							@RequestParam(value = "reportVideo") String reportVideo,
							@RequestParam(value = "reportTime", required = false) String reportTime,
							@RequestParam(value = "supervisorId") String supervisorId) {
		SupervisionReportReq supervisionReportReq = new SupervisionReportReq();
		supervisionReportReq.setOrderId(orderId);
		supervisionReportReq.setReportName(reportName);
		supervisionReportReq.setReportPicture(reportPicture);
		supervisionReportReq.setReportVideo(reportVideo);
		Date report = com.uwo.house.util.DateUtils.getDate(reportTime);
		supervisionReportReq.setReportTime(report);
		supervisionReportReq.setStatus(0);
		supervisionReportReq.setSupervisorId(supervisorId);
		int result = supervisionService.insertReport(supervisionReportReq);
		MeasureOrderResp measureOrderResp = orderMeasureService.getMeasureDetailsByOrderId(orderId);
		String yUserId = "";
		if (null != measureOrderResp) {
			yUserId = measureOrderResp.getUserId();
		}
		if (result == 1 ) {
			DecorationScheduleReq decorationScheduleReq = new DecorationScheduleReq();
			decorationScheduleReq.setOrderId(orderId);
			decorationScheduleReq.setActionDesc("监理上传了我的新房报告");
			decorationScheduleReq.setActionTime(new Date());
			decorationScheduleReq.setCreateTime(new Date());
			decorationSchemeService.addDecorationSchedule(decorationScheduleReq);
			this.addRecord("监理上传了我的新房报告",yUserId,reportPicture,reportVideo);
			return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "上传成功！");
		} else {
			return UtilReturn.resErrorMap("上传失败！",UtilReturn.ERROR_CODE);
		}

	}
	/**
	 * 监理端上传报告完成后
	 * @param orderId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/finish" , method = RequestMethod.POST)
	public Map finish(@RequestParam(value = "orderId") String orderId,
					  @RequestParam(value = "supervisorId") String supervisorId) {
		int result = supervisionService.updateStatus(orderId,supervisorId);
		if (result == 1){
			return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "上传成功！");
		} else {
			return UtilReturn.resErrorMap("上传失败！",UtilReturn.ERROR_CODE);
		}
	}
	/**
	 * 显示监理订单详情
	 * @param supervisorId
	 * @return
	 */
	@RequestMapping(value = "/getReportDetail", method = RequestMethod.POST)
	@ResponseBody
	public Map getReportDetail(@RequestParam(value = "orderId", required = false)String orderId,
							   @RequestParam(value = "supervisorId", required = false) String supervisorId) {
		List<SupervisionReportReq> allList = supervisionService.getReport(orderId,supervisorId);
		return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, allList);
	}
}
